package top.hundreds.oasys.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.hundreds.common.crud.bean.PageParam;
import top.hundreds.common.crud.service.BaseService;
import top.hundreds.oasys.bean.converter.Converter;
import top.hundreds.oasys.bean.entity.Project;
import top.hundreds.oasys.bean.entity.ProjectParticipant;
import top.hundreds.oasys.bean.entity.ProjectPrincipal;
import top.hundreds.oasys.bean.vo.ProjectInfoVo;
import top.hundreds.oasys.bean.vo.ProjectVo;
import top.hundreds.oasys.bean.vo.ReportVo;
import top.hundreds.oasys.bean.vo.UserVo;
import top.hundreds.oasys.mapper.ProjectMapper;
import top.hundreds.oasys.mapper.ProjectParticipantMapper;
import top.hundreds.oasys.mapper.ProjectPrincipalMapper;
import top.hundreds.oasys.mapper.UserMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * (Project)表服务实现类
 *
 * @author lcf
 * @since 2021-01-29 19:19:05
 */
@Service("projectService")
@RequiredArgsConstructor
public class ProjectService extends BaseService<Project, ProjectMapper> {


    private final UserMapper userMapper;

    private final ProjectParticipantMapper projectParticipantMapper;

    private final ProjectPrincipalMapper projectPrincipalMapper;

    private final ReportService reportService;

    private final Converter<Project, ProjectVo> voConverter;

    private final Converter<Project, ProjectInfoVo> infoConverter;

    private final Converter<ProjectInfoVo, Project> converter;

    public ProjectVo toVo(Project project) {
        ProjectVo projectVo = new ProjectVo();
        voConverter.buildVo(project, projectVo);

        return projectVo;
    }

    public ProjectInfoVo toInfoVo(Project project) {
        ProjectInfoVo projectInfoVo = new ProjectInfoVo();
        infoConverter.buildVo(project, projectInfoVo);
        List<Long> participantIds = projectParticipantMapper.getIdListByProjectId(project.getId());
        List<Long> principalIds = projectPrincipalMapper.getIdListByProjectId(project.getId());
        ArrayList<UserVo> participants = new ArrayList<>(participantIds.size());
        ArrayList<UserVo> principals = new ArrayList<>(principalIds.size());
        for (Long participantId : participantIds) {
            UserVo userVo = new UserVo();
            userVo.setUsername(userMapper.getUsernameById(participantId));
            userVo.setId(participantId);
            participants.add(userVo);
        }
        for (Long principalId : principalIds) {
            UserVo userVo = new UserVo();
            userVo.setUsername(userMapper.getUsernameById(principalId));
            userVo.setId(principalId);
            principals.add(userVo);
        }
        List<ReportVo> reportVos = reportService.getListByProjectId(project.getId());
        projectInfoVo.setPrincipalList(principals);
        projectInfoVo.setParticipantList(participants);
        projectInfoVo.setReportVoList(reportVos);
        return projectInfoVo;
    }

    public Page<ProjectVo> toVoPage(Page<Project> page) {
        List<Project> records = page.getRecords();
        ArrayList<ProjectVo> list = new ArrayList<>(records.size());
        for (Project record : records) {
            ProjectVo projectVo = toVo(record);
            projectVo.setCheckName(userMapper.getUsernameById(projectVo.getCheckId()));
            projectVo.setUserName(userMapper.getUsernameById(projectVo.getUserId()));
            projectVo.setStatusName(Integer.valueOf(projectVo.getStatus() == 0 ? "未审核" : "已审核"));
            list.add(projectVo);
        }
        Page<ProjectVo> projectVoPage = new Page<>();
        projectVoPage.setTotal(page.getTotal());
        projectVoPage.setRecords(list);
        projectVoPage.setCurrent(page.getCurrent());
        projectVoPage.setSize(page.getSize());
        return projectVoPage;
    }

    public void remove(Long id) {
        delete(id);
        reportService.deleteByProjectId(id);
        projectParticipantMapper.deleteByProjectId(id);
        projectPrincipalMapper.deleteByProjectId(id);
    }

    public void insertVo(ProjectInfoVo projectInfoVo) {
        Project project = new Project();
        converter.buildVo(projectInfoVo, project);
        insert(project);
        for (UserVo userVo : projectInfoVo.getParticipantList()) {
            projectParticipantMapper.insert(ProjectParticipant.builder().projectId(projectInfoVo.getId()).userId(userVo.getId()).build());
        }
        for (UserVo userVo : projectInfoVo.getPrincipalList()) {
            projectPrincipalMapper.insert(ProjectPrincipal.builder().projectId(projectInfoVo.getId()).userId(userVo.getId()).build());
        }
    }

    public void updateVo(ProjectInfoVo projectInfoVo) {
        Project project = new Project();
        converter.buildVo(projectInfoVo,project);
        update(project);
        projectPrincipalMapper.deleteByProjectId(projectInfoVo.getId());
        projectParticipantMapper.deleteByProjectId(projectInfoVo.getId());
        for (UserVo userVo : projectInfoVo.getParticipantList()) {
            projectParticipantMapper.insert(ProjectParticipant.builder().projectId(projectInfoVo.getId()).userId(userVo.getId()).build());
        }
        for (UserVo userVo : projectInfoVo.getPrincipalList()) {
            projectPrincipalMapper.insert(ProjectPrincipal.builder().projectId(projectInfoVo.getId()).userId(userVo.getId()).build());
        }
    }

    public Page<ProjectVo> myProject(PageParam<ProjectInfoVo> pageParam, Long id) {


        return null;
    }

    public void setStatus(Long id, Integer status) {
        Project project = get(id);
        project.setStatus(status);
        update(project);
    }
}
